/**
 * Filename: BaseActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/2/23 16:27
 */
package com.github.postsanf.yinian;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.github.postsanf.yinian.utils.Logger;
import com.github.postsanf.yinian.utils.ToastUtils;
import com.google.gson.Gson;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Activity基类初始化操作
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected String TAG;                        // 标识
    protected BaseApplication application;                // 应用主体
    protected LiteHttp liteHttp;                    // 网络访问
    protected ImageLoader imageLoader;                // 图片加载
    protected Gson gson;                        // json利器
    protected SharedPreferences sp;                            // 共享数据

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        application = (BaseApplication) getApplication();
        imageLoader = ImageLoader.getInstance();
        gson = new Gson();
        liteHttp = LiteHttp.newApacheHttpClient(new HttpConfig(this));
    }

    // 页面跳转
    protected void intent2Activity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(this, tarActivity);
        startActivity(intent);
    }

    // 显示Toast
    protected void showToast(String msg) {
        ToastUtils.showToast(this, msg, Toast.LENGTH_SHORT);
    }

    // 输出日志
    protected void showLog(String msg) {
        Logger.show(TAG, msg);
    }
}
